/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.database;

import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import java.sql.SQLException;
import log.Debug;

/**
 *
 * @author daz
 */
public class Database {

    IDBManager dbManager;

    public Database(IDBManager dbManager) {
        this.dbManager = dbManager;
    }

    public UserData getUserInfo(String name) {
        UserData userData = new UserData();
        try {
            String sql = "SELECT * FROM users where username='" + name + "';";
            ISFSArray res = dbManager.executeQuery(sql);
            if (res.size() > 0) {
                ISFSObject s = res.getSFSObject(0);
                userData.setId(s.getInt("id"));
                userData.setLevel(s.getInt("level"));
                userData.setLevelName(s.getUtfString("level_name"));
                userData.setMoney(s.getLong("money"));
                userData.setName(s.getUtfString("username"));
            } else {
                return null;
            }
        } catch (SQLException ex) {
            Debug.d(ex.toString());
            return null;
        }
        return userData;
    }

    public void updateUserMoney(String name, long money) {
        try {
            if (money < 0) {
                money = 0;
            }
            String query = "update users set money=" + money + " where username='" + name + "'";
            dbManager.executeUpdate(query);
        } catch (SQLException ex) {
            Debug.d(ex.toString());
        }
    }

    public void insetMoneyQueue(String name, long newMoney, long moneyJoin, String prefix) {
        try {

            deleteMoneyQueue(prefix, name);

            String queueName = "tienlen";
            if (prefix.equals("P")) {
                queueName = "phom";
            } else if (prefix.equals("TLMN")) {
                queueName = "tienlenmiennam";
            } else if (prefix.equals("R")) {
                queueName = "poker";
            }
            String query = "INSERT INTO money_queue_" + queueName + " (username,money) VALUES ('" + name + "'," + moneyJoin + ")";
            dbManager.executeUpdate(query);
            updateUserMoney(name, newMoney);

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public void updateMoneyJoin(String prefix, String name, long newMoney) {
        try {

            String queueName = "tienlen";
            if (prefix.equals("P")) {
                queueName = "phom";
            } else if (prefix.equals("TLMN")) {
                queueName = "tienlenmiennam";
            } else if (prefix.equals("R")) {
                queueName = "poker";
            }
            String query = "update  money_queue_" + queueName + " set money=" + newMoney + " where username='" + name + "'";
            this.dbManager.executeUpdate(query);
        } catch (SQLException ex) {
        }
    }

    public long getMoneyJoin(String roomPrefix, String name) {
        long moneyJoin = 0;
        String queueName = "tienlen";
        if (roomPrefix.equals("P")) {
            queueName = "phom";
        } else if (roomPrefix.equals("TLMN")) {
            queueName = "tienlenmiennam";
        } else if (roomPrefix.equals("R")) {
            queueName = "poker";
        }
        try {
            String query = "select money from money_queue_" + queueName + " where username='" + name + "'";
            ISFSArray res = dbManager.executeQuery(query);
            if (res.size() > 0) {

                for (int i = 0; i < res.size(); i++) {
                    ISFSObject s = res.getSFSObject(i);
                    moneyJoin += s.getLong("money");
                }
                return moneyJoin;

            } else {

                return 0;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return 0;
        }
    }

    public void deleteMoneyQueue(String roomPrefix, String name) {
        try {
            String queueName = "tienlen";
            if (roomPrefix.equals("P")) {
                queueName = "phom";
            } else if (roomPrefix.equals("TLMN")) {
                queueName = "tienlenmiennam";
            } else if (roomPrefix.equals("R")) {
                queueName = "poker";
            }
            String query = "delete from money_queue_" + queueName + " where username='" + name + "'";
            this.dbManager.executeUpdate(query);
        } catch (SQLException ex) {
        }
    }

    public void updateNumberPlayer(String roomPrefix, int userCount) {
        try {
            int id = 0;
            if (roomPrefix.equals("TLMN")) {
                id = 11;
            } else if (roomPrefix.equals("P")) {
                id = 8;
            } else if (roomPrefix.equals("R")) {
                id = 9;
            } else {
                id = 6;
            }
            String query = "update games set player=" + userCount + " where id_game=" + id;
            this.dbManager.executeUpdate(query);
        } catch (SQLException ex) {
        }
    }
}
